package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.entity.SetmealDish;
import com.itheima.reggie.service.SetmealDishService;
import com.itheima.reggie.service.SetmealService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/setmeal")
@Slf4j
@Api(tags = "套餐相关接口")
public class SetmealController {

    @Autowired
    private SetmealService setmealService;
    @Autowired
    private SetmealDishService setmealDishService;
    @Autowired
    private CacheManager cacheManager;

    //    添加套餐和套餐产品
    @PostMapping
    @Transactional
    @CacheEvict(value = "setmeal",key = "#setmealDto.categoryId+'_1'")
    @ApiOperation(value = "新增套餐接口")
    public R<String> save(@RequestBody SetmealDto setmealDto){

        setmealService.saveWithSetmealDish(setmealDto);
        return R.success("操作成功");
    }

//    查询套餐和套餐分类
    @GetMapping("/page")
    @ApiOperation("分页查询")
    public R<Page<SetmealDto>> page(int page, int pageSize, String name){

        Page<SetmealDto> resultPage = setmealService.pageWithCategoryId(page, pageSize, name);
        return R.success(resultPage);
    }

//     批量起售、停售
    @PostMapping("/status/{status}")
    @CacheEvict(value = "setmeal",allEntries = true)
    @ApiOperation("批量停售起售")
    public R<String> setStatus(@PathVariable Integer status,@RequestParam List<Long> ids){
        setmealService.setStatus(status,ids);
        return R.success("起售成功");
    }
// 批量删除、如果有一个起售状态不可以删除
    @DeleteMapping
    @CacheEvict(value = "setmeal",allEntries = true)
    @ApiOperation("删除套餐")
    public R<String> delete(@RequestParam List<Long> ids){
        setmealService.delete(ids);
        return R.success("删除成功");
    }
//    修改套餐时回显数据
    @GetMapping("/{id}")
    public R<SetmealDto> getSetmealUpdate(@PathVariable Long id){
        log.info("接收到前端传来的Id:{}",id);
        SetmealDto setmealDto = setmealService.getSetmealUpdate(id);
        return R.success(setmealDto);
    }

    @PutMapping
    @Transactional
    @CacheEvict(value = "setmeal",allEntries = true)
    @ApiOperation("修改套餐")
    public R<String> updateSetmeal(@RequestBody SetmealDto setmealDto){
//        修改套餐表中的数据
        setmealService.updateById(setmealDto);
//        创建条件构造器
        LambdaQueryWrapper<SetmealDish> setmealDishLqw = new LambdaQueryWrapper<>();
//        设置比较套餐产品表的SetmealId和传来setmealDao中的id是否相等
        setmealDishLqw.eq(SetmealDish::getSetmealId,setmealDto.getId());
//        在setmealDish中设置通过上述条件删除
        setmealDishService.remove(setmealDishLqw);
//        获取到套餐中的所有产品
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
//        遍历setmealDishes并为每一个套餐产品设置所属的套餐id
        setmealDishes.forEach(setmealDish -> setmealDish.setSetmealId(setmealDto.getId()));
//        将套餐中的产品集合添加到套餐产品表
        setmealDishService.saveBatch(setmealDishes);
        return R.success("修改成功");
    }

    @GetMapping("/list")
    @Cacheable(value = "setmeal",key = "#setmeal.categoryId+'_1'")
    @ApiOperation("查询所有套餐")
    public R<List<Setmeal>> list(Setmeal setmeal){
        LambdaQueryWrapper<Setmeal> lqw = new LambdaQueryWrapper<>();
        lqw.eq(setmeal.getCategoryId() != null,Setmeal::getCategoryId,setmeal.getCategoryId());
        lqw.eq(Setmeal::getStatus,1);
        List<Setmeal> list = setmealService.list(lqw);

        return R.success(list);
    }
}
